package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;

import static com.longrise.android.byjk.R.id.rl_invoice1;
import static java.util.Locale.CHINESE;

/**
 * Created by Administrator on 2017/10/23.
 * 发票信息填写界面
 */

public class InvoiceInfoEditActivity extends BaseActivity2 implements TextWatcher, View.OnClickListener {

    public static final String INVOICE_INFO_EDIT = "INVOICE_INFO_EDIT";

    public static final String INVOICE_ID = "INVOICE_ID";

    private TextView tv_invoice_amount;
    private TextView tv_header_type;
    private TextView tv_invoice_type;
    private TextView tv_invoice_header;
    private TextView tv_taxpay_num;
    private TextView tv_invoice_content;
    private TextView tv_open_account;
    private TextView tv_bank_account;
    private TextView tv_company_address;
    private TextView tv_company_tel;
    private TextView tv_company_name;
    private ScrollView sc_info_edit;

    private View view1;
    private View view2;
    private View view3;
    private View view4;

    private  RelativeLayout rl_header_type;
    private  RelativeLayout rl_invoice;
    private  RelativeLayout rl_invoice_header;
    private  RelativeLayout rl_taxpay_num;
    private  RelativeLayout rl_invoice_content;
    private  RelativeLayout rl_open_account_bank;
    private  RelativeLayout rl_account_bank;
    private  RelativeLayout rl_company_address;
    private  RelativeLayout rl_company_tel;
    private RelativeLayout ll_normal_info;
    private RelativeLayout rl_company_name;
    private LinearLayout ll_buy_info;
    private Dialog mInvoiceHeaderDialog;
    private Dialog mInvoiceContentDialog;
    private Dialog mEditContentDialog;
    private Dialog mBuyInfoDialog;
    private Button btn_recipient_edit;

    private String headerType = "1";
    private String invoiceType = "2";
    private String itemType = "";

    private String taxType = "4";
    private String khhType = "5";
    private String bankType = "6";
    private String addType = "7";
    private String telType = "8";
    private String invoiceHaderType = "9";
    private String buyerType = "";
    private String nameType = "11";

    private String mHeaderType;
    private String mInvoiceType;
    private String mInvoiceHeader;
    private String mInvoiceContent;
    private String mTaxpayNum;
    private String mOpenAccount;
    private String mBankAccount;
    private String mAddress;
    private String mConpanyTel;
    private String mConpanyName;

    private String invtype = "";
    private double mInvoiceAmount;

    //发票总金额
    private String amount;
    //发票流水id
    private ArrayList<String> invoiceIds;

    private EditText et_dialog_info;

    private TextView tv_confirm;
    private String invoicePrice;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_invoice_info_edit;
    }


    @Override
    public void initView() {
        tv_invoice_amount = (TextView) findViewById(R.id.tv_invoice_amount);
        tv_header_type = (TextView) findViewById(R.id.tv_header_type);
        tv_invoice_type = (TextView) findViewById(R.id.tv_invoice_type);
        tv_invoice_header = (TextView) findViewById(R.id.tv_invoice_header);
        tv_taxpay_num = (TextView) findViewById(R.id.tv_taxpay_num);
        tv_invoice_content = (TextView) findViewById(R.id.tv_invoice_content);
        tv_open_account = (TextView) findViewById(R.id.tv_open_account);
        tv_bank_account = (TextView) findViewById(R.id.tv_bank_account);
        tv_company_address = (TextView) findViewById(R.id.tv_company_address);
        tv_company_tel = (TextView) findViewById(R.id.tv_company_tel);
        tv_company_name = (TextView) findViewById(R.id.tv_company_name);
        sc_info_edit = (ScrollView) findViewById(R.id.sc_info_edit);

        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        view3 = findViewById(R.id.view3);
        view4 = findViewById(R.id.view4);

        rl_header_type = (RelativeLayout) findViewById(R.id.rl_header_type);
        rl_invoice = (RelativeLayout) findViewById(rl_invoice1);
        rl_invoice_header = (RelativeLayout) findViewById(R.id.rl_invoice_header);
        rl_taxpay_num = (RelativeLayout) findViewById(R.id.rl_taxpay_num);
        rl_invoice_content = (RelativeLayout) findViewById(R.id.rl_invoice_content);
        rl_open_account_bank = (RelativeLayout) findViewById(R.id.rl_open_account_bank);
        rl_account_bank = (RelativeLayout) findViewById(R.id.rl_account_bank);
        rl_company_address = (RelativeLayout) findViewById(R.id.rl_company_address);
        rl_company_tel = (RelativeLayout) findViewById(R.id.rl_company_tel);
        rl_company_name = (RelativeLayout) findViewById(R.id.rl_company_name);
        ll_normal_info = (RelativeLayout) findViewById(R.id.ll_normal_info);
        ll_buy_info = (LinearLayout) findViewById(R.id.ll_buy_info);

        btn_recipient_edit= (Button) findViewById(R.id.btn_recipient_edit);
        btn_recipient_edit.setEnabled(false);
        initEvent();
        getExtraData();
        setToolbarTitle(AppUtil.getString(R.string.invoice_info_edit));
        setToolbarRightTextTitle(AppUtil.getString(R.string.notice_item));
    }


    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        startActivity(InvoiceEditNoticeActivity.class);
    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        invoiceIds = extraIntent.getStringArrayListExtra(InvoiceManagerActivity.INVOICE_ID);
        amount = extraIntent.getStringExtra(InvoiceManagerActivity.INVOICE_AMOUNT);
        mInvoiceAmount = Double.valueOf(amount);
        invoicePrice = String.format(CHINESE, "%.2f", mInvoiceAmount);
        tv_invoice_amount.setText(invoicePrice+"元");
    }

    private void initEvent() {
        rl_header_type.setOnClickListener(this);
        rl_invoice.setOnClickListener(this);
        rl_invoice_header.setOnClickListener(this);
        rl_invoice_content.setOnClickListener(this);
        rl_taxpay_num.setOnClickListener(this);
        rl_open_account_bank.setOnClickListener(this);
        rl_account_bank.setOnClickListener(this);
        rl_company_address.setOnClickListener(this);
        rl_company_tel.setOnClickListener(this);
        rl_company_name.setOnClickListener(this);
        btn_recipient_edit.setOnClickListener(this);

        tv_header_type.addTextChangedListener(this);
        tv_header_type.addTextChangedListener(this);
        tv_invoice_header.addTextChangedListener(this);
        tv_invoice_content.addTextChangedListener(this);
        tv_taxpay_num.addTextChangedListener(this);
        tv_open_account.addTextChangedListener(this);
        tv_bank_account.addTextChangedListener(this);
        tv_company_address.addTextChangedListener(this);
        tv_company_tel.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //抬头类型
            case R.id.rl_header_type:
                itemType = headerType;
                showHintDialog(itemType);
                break;
            //发票类型
            case R.id.rl_invoice1:
                itemType = invoiceType;
                showHintDialog(itemType);
                break;
            //发票抬头
            case R.id.rl_invoice_header:
                buyerType = invoiceHaderType;
                showBuyInfoDialog(buyerType);
                break;
            //纳税识别号
            case R.id.rl_taxpay_num:
                buyerType = taxType;
                showBuyInfoDialog(buyerType);
                break;

            //发票内容
            case R.id.rl_invoice_content:
                showInvoiceContentDialog();
                break;
            //开户行
            case R.id.rl_open_account_bank:
                buyerType = khhType;
                showBuyInfoDialog(buyerType);
                break;
            //银行账号
            case R.id.rl_account_bank:
                buyerType = bankType;
                showBuyInfoDialog(buyerType);
                break;
            //公司名称
            case R.id.rl_company_name:
                buyerType = nameType;
                showBuyInfoDialog(buyerType);
                break;
            //公司地址
            case R.id.rl_company_address:
                buyerType = addType;
                showBuyInfoDialog(buyerType);
                break;
            //公司电话
            case R.id.rl_company_tel:
                buyerType = telType;
                showBuyInfoDialog(buyerType);
                break;

            //点击填写收件信息按钮
            case R.id.btn_recipient_edit:
                if(AppUtil.getString(R.string.heder_type_person).equals(mHeaderType)){
                    clientPersonRequest(invoiceIds,mHeaderType,mInvoiceType,mInvoiceHeader,mInvoiceContent,0,invoicePrice);
                }else if(AppUtil.getString(R.string.heder_type_company).equals(mHeaderType)){
                    if(mInvoiceType.equals(AppUtil.getString(R.string.invoice_type_normal))){
                        clientNormalRequest(invoiceIds,mHeaderType,mInvoiceType,mInvoiceHeader,mTaxpayNum,mInvoiceContent,0,invoicePrice);
                    }else if(mInvoiceType.equals(AppUtil.getString(R.string.invoice_type_special))){
                        clientCompanyRequest(invoiceIds,mHeaderType,mInvoiceType,mInvoiceHeader,mTaxpayNum,mInvoiceContent,0,mOpenAccount,mBankAccount,mAddress,mConpanyTel,invoicePrice,mConpanyName);
                    }

                }
                break;

        }
    }

    //上传抬头信息为公司,抬头为增值税专用发票的发票信息
    private void clientCompanyRequest(ArrayList<String> invoiceIds,String customertype, String invtype,
                                      String customername,String zzs_taxno,
                                      String invoiceitem,int taxpayertype ,
                                      String zzs_openbyzbank,String zzs_bankcardno,
                                      String zzs_address,String zzs_phone,String amt, String zzs_company_name) {
        EntityBean serBean = new EntityBean();
        EntityBean bean = new EntityBean();
        serBean.set("customertype", customertype);
        serBean.set("invtype", invtype);
        serBean.set("customername", customername);
        serBean.set("zzs_taxno", zzs_taxno);
        serBean.set("invoiceitem", invoiceitem);
        serBean.set("taxpayertype", taxpayertype);
        serBean.set("zzs_openbyzbank", zzs_openbyzbank);
        serBean.set("zzs_bankcardno", zzs_bankcardno);
        serBean.set("zzs_address", zzs_address);
        serBean.set("zzs_phone", zzs_phone);
        serBean.set("amt", amt);
        serBean.set("zzs_companyname", zzs_company_name);
        bean.set("chargelistids", invoiceIds);
        bean.set("invoicebean", serBean);
        openRecipientInfo(bean);

    }

    //上传抬头信息为个人的发票信息
    private void clientPersonRequest(ArrayList<String> invoiceIds,String customertype, String invtype, String customername,
                                     String invoiceitem,int taxpayertype,String amt) {
        EntityBean personbean = new EntityBean();
        EntityBean bean = new EntityBean();
        personbean.set("taxpayertype", taxpayertype);
        personbean.set("amt", amt);
        personbean.set("customertype", customertype);
        personbean.set("invtype", invtype);
        personbean.set("customername", customername);
        personbean.set("invoiceitem", invoiceitem);
        bean.set("chargelistids", invoiceIds);
        bean.set("invoicebean", personbean);
        openRecipientInfo(bean);
    }

    //上传抬头信息为公司，发票类型为普通发票的发票信息
    private void clientNormalRequest(ArrayList<String> invoiceIds,String customertype, String invtype, String customername,String zzs_taxno,
                                     String invoiceitem,int taxpayertype,String amt) {
        EntityBean normalbean = new EntityBean();
        EntityBean bean = new EntityBean();
        normalbean.set("taxpayertype", taxpayertype);
        normalbean.set("amt", amt);
        normalbean.set("customertype", customertype);
        normalbean.set("invtype", invtype);
        normalbean.set("customername", customername);
        normalbean.set("zzs_taxno", zzs_taxno);
        normalbean.set("invoiceitem", invoiceitem);
        bean.set("chargelistids", invoiceIds);
        bean.set("invoicebean", normalbean);
        openRecipientInfo(bean);
    }


    public boolean checkPersonText(){
        mHeaderType = tv_header_type.getText().toString();
        mInvoiceType = tv_invoice_type.getText().toString();
        mInvoiceHeader = tv_invoice_header.getText().toString();
        mInvoiceContent = tv_invoice_content.getText().toString();
        if(TextUtils.isEmpty(mHeaderType)){
            return false;
        }

        if(TextUtils.isEmpty(mInvoiceType)){
            return false;
        }
        if(TextUtils.isEmpty(mInvoiceHeader)){
            return false;
        }

        if(TextUtils.isEmpty(mInvoiceContent)){
            return false;
        }
        return true;
    }


    public boolean checkCompanyText(){

        mHeaderType = tv_header_type.getText().toString();
        mInvoiceType = tv_invoice_type.getText().toString();
        mInvoiceHeader = tv_invoice_header.getText().toString();
        mInvoiceContent = tv_invoice_content.getText().toString();
        mTaxpayNum = tv_taxpay_num.getText().toString();
        mOpenAccount = tv_open_account.getText().toString();
        mBankAccount = tv_bank_account.getText().toString();
        mAddress = tv_company_address.getText().toString();
        mConpanyTel = tv_company_tel.getText().toString();
        mConpanyName = tv_company_name.getText().toString();
        if(TextUtils.isEmpty(mHeaderType)){
            return false;
        }

        if(TextUtils.isEmpty(mInvoiceType)){
            return false;
        }
        if(TextUtils.isEmpty(mInvoiceHeader)){
            return false;
        }

        if(TextUtils.isEmpty(mInvoiceContent)){
            return false;
        }

        if(TextUtils.isEmpty(mTaxpayNum)){
            return false;
        }

        if(mInvoiceType.equals(AppUtil.getString(R.string.invoice_type_special))){
            if(TextUtils.isEmpty(mOpenAccount)){
                return false;
            }

            if(TextUtils.isEmpty(mBankAccount)){
                return false;
            }

            if(TextUtils.isEmpty(mConpanyName)){
                return false;
            }

            if(TextUtils.isEmpty(mAddress)){
                return false;
            }

            if(TextUtils.isEmpty(mConpanyTel)){
                return false;
            }
        }
        return true;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String mHeaderType = tv_header_type.getText().toString();
        if(mHeaderType.equals(AppUtil.getString(R.string.heder_type_person))){
            rl_header_type.setBackgroundResource(R.drawable.by_item_selecter_bg);
            rl_invoice.setVisibility(View.VISIBLE);
            rl_invoice_header.setVisibility(View.VISIBLE);
            rl_invoice_content.setVisibility(View.VISIBLE);
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            view3.setVisibility(View.VISIBLE);
            view4.setVisibility(View.GONE);
            rl_taxpay_num.setVisibility(View.GONE);
            ll_buy_info.setVisibility(View.GONE);
            rl_invoice.setClickable(false);
            tv_invoice_type.setPadding(0, 0, AppUtil.dip2px(20), 0);
            tv_invoice_type.setCompoundDrawables(null,null,null,null);
            tv_invoice_type.setText(AppUtil.getString(R.string.invoice_type_normal));
            tv_invoice_header.setHint(AppUtil.getString(R.string.header_hint));
            boolean b = checkPersonText();
            if (b) {
                btn_recipient_edit.setEnabled(true);
            } else {
                btn_recipient_edit.setEnabled(false);
            }
        }else if(mHeaderType.equals(AppUtil.getString(R.string.heder_type_company))){
            rl_header_type.setBackgroundResource(R.drawable.by_item_selecter_bg);
            rl_invoice.setVisibility(View.VISIBLE);
            rl_invoice_header.setVisibility(View.VISIBLE);
            rl_taxpay_num.setVisibility(View.VISIBLE);
            rl_invoice_content.setVisibility(View.VISIBLE);
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            view3.setVisibility(View.VISIBLE);
            view4.setVisibility(View.VISIBLE);
            Drawable drawable= getResources().getDrawable(R.drawable.by_ic_more);
            /// 这一步必须要做,否则不会显示.
            tv_invoice_type.setPadding(0, 0, 0, 0);
            drawable.setBounds(0, 0, AppUtil.dip2px(20), AppUtil.dip2px(20));
            tv_invoice_type.setCompoundDrawables(null, null, drawable, null);
            tv_invoice_header.setHint(AppUtil.getString(R.string.header_company));
            rl_invoice.setClickable(true);
            boolean b = checkCompanyText();
            if (b) {
                btn_recipient_edit.setEnabled(true);
            } else {
                btn_recipient_edit.setEnabled(false);
            }
        }

         if(buyerType == bankType||buyerType == telType||buyerType==addType||buyerType == nameType){
             tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
        }

    }


    public void showHintDialog(final String type){
        View mHeaderDialog = View.inflate(mContext, R.layout.dialog_invoice_header, null);
        final RadioButton rb_check1 = (RadioButton) mHeaderDialog.findViewById(R.id.rb_check1);
        final RadioButton rb_check2 = (RadioButton) mHeaderDialog.findViewById(R.id.rb_check2);
        TextView tv_invoice_hint1= (TextView) mHeaderDialog.findViewById(R.id.tv_invoice_hint1);
        TextView tv_invoice_hint2= (TextView) mHeaderDialog.findViewById(R.id.tv_invoice_hint2);
        TextView tv_invoice_type1= (TextView) mHeaderDialog.findViewById(R.id.tv_invoice_type1);
        TextView tv_invoice_type2= (TextView) mHeaderDialog.findViewById(R.id.tv_invoice_type2);
        RelativeLayout rl_item1= (RelativeLayout) mHeaderDialog.findViewById(R.id.rl_item1);
        RelativeLayout rl_item2= (RelativeLayout) mHeaderDialog.findViewById(R.id.rl_item2);

        if(type == headerType){
            tv_invoice_type1.setText(AppUtil.getString(R.string.heder_type_company));
            tv_invoice_type2.setText(AppUtil.getString(R.string.heder_type_person1));
            tv_invoice_hint1.setText(AppUtil.getString(R.string.invoice_hint_common));
            tv_invoice_hint2.setText(AppUtil.getString(R.string.invoice_hint_normal));
            if(!TextUtils.isEmpty(mHeaderType)){
                if(mHeaderType.equals(AppUtil.getString(R.string.heder_type_company))){
                    rb_check1.setChecked(true);
                }else if(mHeaderType.equals(AppUtil.getString(R.string.heder_type_person))){
                    rb_check2.setChecked(true);
                }
            }
        }else if(type == invoiceType){
            String  invoiceType = tv_invoice_type.getText().toString();
            tv_invoice_type1.setText(AppUtil.getString(R.string.invoice_type_normal));
            tv_invoice_type2.setText(AppUtil.getString(R.string.invoice_type_special));
            tv_invoice_hint1.setText(AppUtil.getString(R.string.header_hint_person));
            tv_invoice_hint2.setText(AppUtil.getString(R.string.header_hint_company));
            if(!TextUtils.isEmpty(invoiceType)){
                if(mHeaderType.equals(AppUtil.getString(R.string.heder_type_company))){
                    if(invoiceType.equals(AppUtil.getString(R.string.invoice_type_normal))){
                        rb_check1.setChecked(true);
                    }else if(invoiceType.equals(AppUtil.getString(R.string.invoice_type_special))){
                        rb_check2.setChecked(true);
                    }
                }

            }
        }

        rl_item1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_check1.setChecked(true);
            }
        });

        rl_item2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_check2.setChecked(true);
            }
        });

        mInvoiceHeaderDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mHeaderDialog, 311, 160);
        mInvoiceHeaderDialog.setCanceledOnTouchOutside(true);
        rb_check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                if(type == headerType) {
                    DialogUtil.getInstance().dismiss();
                    tv_header_type.setText(AppUtil.getString(R.string.heder_type_company));
                }else if(type == invoiceType){
                    DialogUtil.getInstance().dismiss();
                    tv_invoice_type.setText(AppUtil.getString(R.string.invoice_type_normal));
                    ll_buy_info.setVisibility(View.GONE);
                }
            }
        });

        rb_check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                if(type == headerType) {
                    tv_header_type.setText(AppUtil.getString(R.string.heder_type_person));
                }else if(type == invoiceType){
                    tv_invoice_type.setText(AppUtil.getString(R.string.invoice_type_special));
                    ll_buy_info.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //填写购买方信息/纳税的dialog
    public void showBuyInfoDialog(final String type){

        final View mEditDialog = View.inflate(mContext, R.layout.dialog_invoice_edit, null);
        final TextView tv_dialog_title= (TextView) mEditDialog.findViewById(R.id.tv_dialog_title);
        final TextView tv_dialog_notice= (TextView) mEditDialog.findViewById(R.id.tv_dialog_notice);
        et_dialog_info= (EditText) mEditDialog.findViewById(R.id.et_dialog_info);
        TextView tv_cancel= (TextView) mEditDialog.findViewById(R.id.tv_cancel);
        tv_confirm= (TextView) mEditDialog.findViewById(R.id.tv_confirm);
        final LinearLayout ll_recipient_content  = (LinearLayout)mEditDialog.findViewById(R.id.ll_recipient_content);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_recipient_content.getLayoutParams();

        //纳税识别号
        if( type == taxType){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.tax_num));
            tv_dialog_notice.setVisibility(View.GONE);
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);

            et_dialog_info.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                                   int dstart, int dend) {
                            String s = source.toString();
                            if(s.matches(AppUtil.getString(R.string.taxpayer_limit))){
                                tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
                                return source;
                            }
                            return "";
                        }
                    }
            });

            if(!TextUtils.isEmpty(mTaxpayNum)){
                et_dialog_info.setText(mTaxpayNum);
                et_dialog_info.setSelection(mTaxpayNum.length());
            }
        }else if( type == khhType ){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.open_account));
            tv_dialog_notice.setVisibility(View.GONE);
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_TEXT);
            et_dialog_info.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                                   int dstart, int dend) {
                            if(source.toString().matches("[\u4e00-\u9fa5]+")){
                                tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
                                return source;
                            }
                            return "";
                        }
                    }
            });
            if(!TextUtils.isEmpty(mOpenAccount)){
                et_dialog_info.setText(mOpenAccount);
                et_dialog_info.setSelection(mOpenAccount.length());
            }
        }else if( type == bankType ){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.bank_account));
            tv_dialog_notice.setVisibility(View.GONE);
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_NUMBER);
            tv_confirm.setTextColor(Color.parseColor("#FF818181"));
            if(!TextUtils.isEmpty(mBankAccount)){
                et_dialog_info.setText(mBankAccount);
                et_dialog_info.setSelection(mBankAccount.length());
            }
        }else if( type == telType ){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.company_tel));
            tv_dialog_notice.setVisibility(View.GONE);
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_NUMBER);
            tv_confirm.setTextColor(Color.parseColor("#FF818181"));
            if(!TextUtils.isEmpty(mConpanyTel)){
                et_dialog_info.setText(mConpanyTel);
                et_dialog_info.setSelection(mConpanyTel.length());
            }
        }else if(type == addType){
            tv_dialog_title.setText(AppUtil.getString(R.string.company_add));
            tv_dialog_notice.setText(AppUtil.getString(R.string.address_notice));
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 211);
            tv_confirm.setTextColor(Color.parseColor("#FF818181"));
            if(!TextUtils.isEmpty(mInvoiceHeader)){
                et_dialog_info.setText(mAddress);
                et_dialog_info.setSelection(mAddress.length());
            }
        }else if(type == invoiceHaderType){
            tv_dialog_title.setText(AppUtil.getString(R.string.invoice_header));
            tv_dialog_notice.setText(AppUtil.getString(R.string.invoice_header_nptoce));
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 211);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_TEXT);
            et_dialog_info.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                                   int dstart, int dend) {

                            if(source.toString().matches("[\u4e00-\u9fa5]+")){
                                tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
                                return source;
                            }
                            return "";
                        }
                    }
            });
            if(!TextUtils.isEmpty(mInvoiceHeader)){
                et_dialog_info.setText(mInvoiceHeader);
                et_dialog_info.setSelection(mInvoiceHeader.length());
            }
        }else if(type == nameType){     //公司地址
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.company_name));
            tv_dialog_notice.setVisibility(View.GONE);
            mBuyInfoDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            tv_confirm.setTextColor(Color.parseColor("#FF818181"));
            if(!TextUtils.isEmpty(mConpanyName)){
                et_dialog_info.setText(mConpanyName);
                et_dialog_info.setSelection(mConpanyName.length());
            }
        }


        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dialigInfo = et_dialog_info.getText().toString();
                if(!TextUtils.isEmpty(dialigInfo)){
                    if( type == taxType){
                        if(dialigInfo.length()==15||dialigInfo.length()==18||dialigInfo.length()==20){
                            tv_taxpay_num.setText(dialigInfo);
                            DialogUtil.getInstance().dismiss();
                        }else{
                            showToast(getString(R.string.taxpay_hint));
                        }
                    }else if( type == khhType ){
                        tv_open_account.setText(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }else if(type == bankType){
                        tv_bank_account.setText(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }else if(type == telType){
                        tv_company_tel.setText(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }else if(type == addType){
                        tv_company_address.setText(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }else if(type == invoiceHaderType){
                        tv_invoice_header.setText(dialigInfo);
                        sc_info_edit.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
                        DialogUtil.getInstance().dismiss();
                    }else if(type == nameType){
                        tv_company_name.setText(dialigInfo);
                        DialogUtil.getInstance().dismiss();
                    }
                }
            }
        });


        et_dialog_info.addTextChangedListener(this);
        mBuyInfoDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mBuyInfoDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    //发票内容的dialog
    public void showInvoiceContentDialog(){
        View mContentDialog = View.inflate(mContext, R.layout.dialog_invoice_content, null);
        final RadioButton rb_content_check1 = (RadioButton) mContentDialog.findViewById(R.id.rb_content_check1);
        final RadioButton rb_content_check2 = (RadioButton) mContentDialog.findViewById(R.id.rb_content_check2);
        RelativeLayout rl_content_item1= (RelativeLayout) mContentDialog.findViewById(R.id.rl_content_item1);
        RelativeLayout rl_content_item2= (RelativeLayout) mContentDialog.findViewById(R.id.rl_content_item2);

        if(!TextUtils.isEmpty(mInvoiceContent)){
            if(mInvoiceContent.equals(AppUtil.getString(R.string.invoice_content_service))){
                rb_content_check1.setChecked(true);
            }else if(mInvoiceContent.equals(AppUtil.getString(R.string.invoice_content_course))){
                rb_content_check2.setChecked(true);
            }
        }

        rl_content_item1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_content_check1.setChecked(true);
            }
        });

        rl_content_item2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_content_check2.setChecked(true);
            }
        });

        mInvoiceContentDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mContentDialog, 311, 183);
        mInvoiceContentDialog.setCanceledOnTouchOutside(true);
        rb_content_check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                tv_invoice_content.setText(AppUtil.getString(R.string.invoice_content_service));
            }
        });

        rb_content_check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DialogUtil.getInstance().dismiss();
                tv_invoice_content.setText(AppUtil.getString(R.string.invoice_content_course));
            }
        });


    }


    public void openRecipientInfo(EntityBean bean){
        Intent intent = new Intent(this,RecipientInfoActivity.class);
        intent.putStringArrayListExtra(INVOICE_ID,invoiceIds);
        String entityBean = JSONSerializer.getInstance().Serialize(bean);
        intent.putExtra(INVOICE_INFO_EDIT,entityBean);
        startActivity(intent);
    }

    private void softKey(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        //请求获得焦点
        editText.requestFocus();
        InputMethodManager inputManager = (InputMethodManager) editText
                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(editText, 0);
    }
}
